package billrestauration.extra.controllers;
 




import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;  
import org.springframework.validation.BindingResult;  
import org.springframework.web.bind.annotation.ModelAttribute;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.multipart.MultipartFile;  
import org.springframework.web.servlet.ModelAndView;  

import billrestaurant.extra.upload.FileUpload;
import billrestaurant.extra.upload.FileValidator;

 
@Controller  
public class UploadController {  
	 @Value("${upload.path}")
	  String path;
 @Autowired  
 FileValidator fileValidator;  
  
 @RequestMapping("/fileUploadForm")  
 public ModelAndView getUploadForm(  
   @ModelAttribute("uploadedFile") FileUpload uploadedFile,  
   BindingResult result) {  
  return new ModelAndView("uploadForm");  
 }  
  
  
@RequestMapping("/fileUpload")  
 public ModelAndView fileUploaded(  
   @ModelAttribute("uploadedFile") FileUpload uploadedFile,  
   BindingResult result) throws Exception {  
   
  MultipartFile file = uploadedFile.getFile();  
  fileValidator.validate(uploadedFile, result);  
  if (result.hasErrors()) {  
	   return new ModelAndView("uploadForm");  
	  }  
 
  uploadedFile.move(path,file.getOriginalFilename());
  return new ModelAndView("showFile", "message", file.getOriginalFilename());  
 }  
  
}  